//
//  CollectViewController.m
//  1243
//
//  Created by 王 on 16/6/3.
//  Copyright © 2016年 1243. All rights reserved.
//

#import "CollectViewController.h"
#import "CollectCell.h"
#import "NetWorkTools.h"
#import "ShopMainViewController.h"

@interface CollectViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *collectArrM;

@end

@implementation CollectViewController

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStyleGrouped];
    }
    return _tableView;
}

- (NSMutableArray *)collectArrM {
    if (!_collectArrM) {
        _collectArrM = [NSMutableArray array];
    }
    return _collectArrM;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"我的收藏";
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"CollectCell" bundle:nil] forCellReuseIdentifier:@"CollectCell"];
    
    [self.view addSubview:self.tableView];
    
//    [self loadCollectList];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    [self loadCollectList];
}

- (void)loadCollectList {
    
    //ri.MenthodName = "USERCOLLECTLIST";
    //uu.U_Number = 1002;
    
    [self.collectArrM removeAllObjects];
    
    NSDictionary *dict = [ReadLoginData readLoginData];
    
    NSString *userId = [dict objectForKey:@"Number"];
    NSDictionary *params = @{
                             @"U_Number":userId
                             };
    [[NetWorkTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"USERCOLLECTLIST" completeBlock:^(id object) {
        NSLog(@"%@", object);
        if ([[NSString stringWithFormat:@"%@",object[@"Id"]] isEqualToString:@"1"]) {
            
        }else{
            [self.collectArrM addObjectsFromArray:[NetWorkTools arrayWithJsonString:object[@"BackData"]]];

        }
 
        [self.tableView reloadData];
        
    }];

}

#pragma mark - UITableView DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.collectArrM.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CollectCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CollectCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.imageV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://120.76.162.124:8083%@", self.collectArrM[indexPath.section][@"PictureURL"]]]];
    cell.title.text = self.collectArrM[indexPath.section][@"Title"];
    cell.price.text = [NSString stringWithFormat:@"￥%@", self.collectArrM[indexPath.section][@"SellPrice"]];
    
    return cell;
}

#pragma mark - UITableView Delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 140;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ShopMainViewController *shopMainVC = [[ShopMainViewController alloc] init];
    shopMainVC.xingYingShopID = self.collectArrM[indexPath.section][@"Id"];
    
    [self.navigationController pushViewController:shopMainVC animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
